﻿using DataAccess;
using KhoaLuan.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;

namespace KhoaLuan.Account
{

    public partial class Login : System.Web.UI.Page
    {
        ILog log = LogManager.GetLogger(typeof(DataAccess.tbLog).Name);
        ////ILog log = LogManager.GetLogger(typeof(Login).Name);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                log.Info(" login thanh cong vao he thong");
                if (UserLogin.IsLogin)
                {
                    var url = Request.QueryString["returnUrl"];
                    if (url != null)
                        Response.Redirect(url);
                    else
                        Response.Redirect("~/MainSys/Manager/Admin.aspx");
                }
                tbOrg_Proxy orgproxy = new tbOrg_Proxy();
                var listorg = orgproxy.SelectAll_tbOrg_Proxy().ToList<tbOrg>();
                cbOrg.DataSource = listorg;
                cbOrg.DataTextField = "NameOrg";
                cbOrg.DataValueField = "OrgID";
                cbOrg.DataBind();
            }
        }
        /// <summary>
        /// login system
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="remember"></param>
        /// <returns></returns>
        [WebMethod]
        public static string LoginSystem(int orgid, string username, string password, bool remember)
        {
            try
            {
                tbUsers_Proxy user = new tbUsers_Proxy();
                object[] ob = { username, EncryptData.Encryption(password) };
                tbUsers us = user.GetById_tbUsers_Proxy(ob);
                if (us != null)
                {
                    //kiem tra co dc dang nhap vao org do hay khong
                    UserLogin.SetCookies(us.UserID + "_" + orgid, HttpContext.Current.Response, remember);
                    return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(new { Status = true });
                }
                else
                {
                }
                return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(new { Status = false });
            }
            catch (Exception)
            {
                return "";
            }
        }
    }

}